<script type="text/javascript">
$(function(){
	var pg = App.office.workList.ui, pp = pg.params || {}
		, hd = pg.children('.head')
		, bd = pg.children('.body')
		, gd = bd.find('.grid')
		, ttLb = hd.seek('title');

	var frm = {
		type: hd.seek('type'),
		status: hd.seek('status'),
		owner: hd.seek('owner'),
		group: hd.seek('group')
	}
	frm.type.mac('combo', {
		width: 121,
		boxWidth: 122,
		readOnly: 1,
		data: Cache.office.work.type
	});
	frm.status.mac('combo', {
		width: 81,
		boxWidth: 82,
		readOnly: 1,
		data: Cache.office.work.status
	}).val(0);
	var userGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'code', title : '编号', width: 120
		},{
			field: 'name', title : '姓名', width: 240
		}],
		loader: {
			url: App.auth.ctx + '/sys/user/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.owner.mac('combo', {
		keyField: 'id',
		displayField: 'name',
		multiSelect: false,
		width: 96,
		boxWidth: 400,
		boxHeight: 300,
		boxEl: userGd,
		onSelect: function(me, sk, sd){
			//var r = sd[0];
			//hd.owner.text(r ? r.name : '');
		}
	});
	frm.owner.mac('combo_grid', {});
	
	var groupGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'name', title: '工作组名称', width: 360, sort: true
		}],
		loader: {
			url: App.office.ctx + '/group/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.group.mac('combo', {
		keyField: 'id',
		displayField: 'name',
		multiSelect: false,
		width: 200,
		boxWidth: 360,
		boxHeight: 300,
		boxEl: groupGd,
		onSelect: function(me, sk, sd){
			//var r = sd[0];
			//hd.owner.text(r ? r.name : '');
		}
	});
	frm.group.mac('combo_grid', {});

	gd.mac('grid', {
		cols : [{
			field: 'title', title : '任务名', width: 240, sort: true,
			render: function(r){
				return $('<span class="a"></span>').click(function(){
					App.open({
						title: '编辑工作任务',
						ctx: 'office',
						action: 'workEdit',
						params: r,
						model : true
					}, gd.load);
					return false;
				}).text(r.title);
			}
		},{
			field: 'owner', title: '负责人', width: 80, sort: true,
			render: function(r){
				return $('<span></span>').click(function(){
					hd.owner.val(r);
					gd.load({ ownerId: r.ownerId });
				}).append(r.owner.name);
			}
		},{
			field: 'planStartDate', title: '计划开始时间', width: 120, sort: true
		},{
			field: 'planEndDate', title: '计划结束时间', width: 120, sort: true
		},{
			field: 'actualStartDate', title: '实际开始时间', width: 120, sort: true
		},{
			field: 'actualEndDate', title: '实际结束时间', width: 120, sort: true
		},{
			field: 'status', title: '状态', width: 60,
			render: function(r){
				return Cache.office.work.status[r.status];
			}
		},{
			field: 'progress', title: '进度', width: 60,
			render: function(r){
				return r.progress + '%';
			}
		},{
			field: 'id', title: '操作', width: 48,
			render: function(r, tr, me){
				var a = $('<div></div>'), s = r.status;
				if (s == 4 || s == 5) {
					if (App.logon.power('office.work.close')) {
						$('<span class="a"></span>').click(function(){
							mac.confirm('确定要关闭此任务吗？关闭后此任务将不再显示！', function(){
								$.get(App.office.ctx + '/work/close.do', {
									id: r.id
								}, function(rs){
									var ro = mac.eval(rs);
									if (ro.success) {
										gd.load();
										return;
									}
									mac.alert(ro.data, ro.params);
								});
							});
						}).append('关闭').appendTo(a);
					}
				}
				if (s == 1 || s == 2 || s == 3) {
					if (App.logon.power('office.work.cancel')) {
						$('<span class="a"></span>').click(function(){
							mac.confirm('确定要取消此任务吗？', function(){
								$.get(App.office.ctx + '/work/cancel.do', {
									id: r.id
								}, function(rs){
									var ro = mac.eval(rs);
									if (ro.success) {
										gd.load();
										return;
									}
									mac.alert(ro.data, ro.params);
								});
							});
						}).append('取消').appendTo(a);
					}
				}
				return a;
			}
		}],
		loader: {
			url: App.office.ctx + '/work/list.do',
			params: { pageNo: 1, pageSize: 20, status: 0 },
			autoLoad: true
		},
		pagerLength: 10,
		onLoadFailed: function(dd) {
			//
		}
	});

	pg.seek('find').click(function(){
		gd.load({
			key: pg.seek('key').val(),
			pageNo: 1,
			groupId: frm.group.val() || '', // undefined时不会被覆盖
			ownerId: frm.owner.val() || '',
			status: frm.status.val() || ''
		});
	});
	pg.seek('key').keypress(function(e){
		if (e.keyCode == 13) {
			pg.seek('find').click();
		}
	});
	
	if (App.logon.power('office.work.add')) {
		hd.seek('add').click(function(){
			App.open({
				title: '添加任务',
				ctx: 'office',
				action: 'workEdit',
				params: { owner: App.logon }
			}, function(){
				gd.load();
			});
		}).show();
	}
	
	pg.seek('back').click(function(){
		ttLb.text('主页');
		App.show({
			title: '主页',
			ctx: 'office',
			action: 'work',
			params: {}
		}, $.noop, pg.parent());
	});
	
	function adjust(){
		var p = pg.parent(), h = p.height(), w = p.width() - 16;
		pg.height(h).width(w);
		h -= pg.children('.head').height();
		h -= pg.children('.navi').height();
		bd.height(h - 24)
		gd.adjust();
	}
	$(window).resize(adjust);
	adjust();
	
	pg.find('[placeholder]').mac('placeholder');
});
</script>
<div class="navi">
	<span class="a" name="back">办公管理</span>&gt;&gt; <span class="a"
	name="title">工作任务列表</span>
</div>
<div class="head">
	<button name="add" class="hidden fl">添加新模块</button>
	<div class="form">
		<div class="fl">关键字：</div>
		<input class="fl" name="key" type="text" placeholder="请输入模块名" />
		<div class="fl">&nbsp;&nbsp;工作组：</div>
		<div class="combo fl" name="group"></div>
		<div class="fl">&nbsp;&nbsp;状态：</div>
		<div class="combo fl" name="status"></div>
		<div class="fl">&nbsp;&nbsp;负责人：</div>
		<div class="combo fl" name="owner"></div>
	</div>
	<button class="fl" name="find">查找</button>
</div>
<div class="body">
	<div class="grid"></div>
</div>
